package com.boarsoft.boar.batch.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.boarsoft.boar.batch.bean.BatchSchedule;
import com.boarsoft.boar.batch.biz.BatchInfoBiz;
import com.boarsoft.boar.batch.quartz.QuartzSchedulerMonitor;
import com.boarsoft.boar.batch.service.BatchScheduler;
import com.boarsoft.boar.common.Constants;
import com.boarsoft.common.Authorized;
import com.boarsoft.common.Util;
import com.boarsoft.common.action.PageAction;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.common.util.JsonUtil;

@SuppressWarnings("serial")
@Component
@Scope("prototype")
@ParentPackage("default-package")
@Namespace(value = "/schedule")
public class BatchScheduleAction extends PageAction {

	@Autowired
	private BatchScheduler scheduleBroker;

	@Autowired(required = false)
	@Qualifier("schedulerMonitor")
	private QuartzSchedulerMonitor monitor;
	
	@Autowired
	private BatchInfoBiz infoBiz;

	private String id;

	private String impl;

	private String catalog;

	private String key;

	@Action("list")
	@Authorized(code = "batch.schedule.list")
	public void list() {
		try {
			List<BatchSchedule> rLt = new ArrayList<BatchSchedule>();
			List<BatchSchedule> aLt = scheduleBroker.list();
			if (Util.strIsEmpty(catalog)) {
				rLt.addAll(aLt);
			} else {
				for (BatchSchedule o : aLt) {
					int i = -1;
					if (Util.strIsNotEmpty(catalog)) {
						if (o.getCatalog().equalsIgnoreCase(catalog)) {
							i = 0;
						} else {
							continue;
						}
					}
					if (Util.strIsEmpty(key)) {
						i = 0;
					} else {
						i = o.getCode().toLowerCase().indexOf(key.toLowerCase());
					}
					if (i < 0) {
						continue;
					}
					rLt.add(o);
				}
			}
			PagedResult<BatchSchedule> pr = new PagedResult<BatchSchedule>(//
					rLt.size(), rLt, pageNo, pageSize);
			writeReply(true, JsonUtil.from(pr));
		} catch (Exception e) {
			log.error("Error on list batch schedule", e);
			writeReply(Constants.ERROR_UNKNOWN);
		}
	}

	@Action("start")
	@Authorized(code = "batch.schedule.start")
	public void start() {
		try {
			if (monitor != null)
				monitor.enableGroup();// 标记BatchLock.status为1-启用
			else
				scheduleBroker.start();
			writeReply();
		} catch (Exception e) {
			log.error("Error on start quartz scheduler", e);
			writeReply(Constants.ERROR_UNKNOWN);
		}
	}

	@Action("stop")
	@Authorized(code = "batch.schedule.stop")
	public void stop() {
		try {
			if (monitor != null)
				monitor.disableGroup();// 标记BatchLock.status为0-停用
			else
				scheduleBroker.stop();
			writeReply();
		} catch (Exception e) {
			log.error("Error on stop quartz scheduler", e);
			writeReply(Constants.ERROR_UNKNOWN);
		}
	}

	@Action("delete")
	@Authorized(code = "batch.schedule.delete")
	public void delete() {
		if (Util.strIsEmpty(id) || Util.strIsEmpty(impl)) {
			writeReply(Constants.ERROR_INVALID, "id,impl");
			return;
		}
		try {
			scheduleBroker.unschedule(id, impl);
			writeReply();
		} catch (Exception e) {
			log.error("Error on stop quartz scheduler", e);
			writeReply(Constants.ERROR_UNKNOWN);
		}
	}

	/**
	 * 返回定时器是否已启动（暂停模式）
	 */
	@Action("status")
	public void status() {
		boolean b;
		if (monitor != null)
			b = !monitor.groupStatus();
		else
			b = scheduleBroker.isStandByMode();
		writeReply(true, b ? "0" : "1");
	}

	public BatchInfoBiz getInfoBiz() {
		return infoBiz;
	}

	public void setInfoBiz(BatchInfoBiz infoBiz) {
		this.infoBiz = infoBiz;
	}

	public String getCatalog() {
		return catalog;
	}

	public void setCatalog(String catalog) {
		this.catalog = catalog;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getImpl() {
		return impl;
	}

	public void setImpl(String impl) {
		this.impl = impl;
	}

	public BatchScheduler getScheduleBroker() {
		return scheduleBroker;
	}

	public void setScheduleBroker(BatchScheduler scheduleBroker) {
		this.scheduleBroker = scheduleBroker;
	}
}
